<?php
require_once 'includes/class.phpmailer.php';

if($_POST)
{
$Subject=$_POST['txtSub'];
$Name=$_POST['txtName'];
$Email=$_POST['txtEmail'];
$Username=$_POST['txtUser'];
$Password=$_POST['txtPass'];
$Query=addslashes($_POST['txtQuery']);

$emailContent="Subject: ".$_POST['txtSub']."<br>";
$emailContent.="Name: ".$_POST['txtName']."<br>";
$emailContent.="Email: ".$_POST['txtEmail']."<br>";
$emailContent.="Username: ".$_POST['txtUser']."<br>";
$emailContent.="Password: ".$_POST['txtPass']."<br>";
$emailContent.="Query: ".$_POST['txtQuery']."<br>";

$sqlAddcontact="INSERT INTO tbl_report set subject='$Subject', name='$Name', email='$Email', username='$Username', password='$Password',comment='$Query',post_date='".date('Y-m-d')."'";
$rstAddcontact=mysql_query($sqlAddcontact) or die(mysql_error());
$insertId=mysql_insert_id();

	 $subject="Thank you for contacting Swapen";
	 $sqlEmail="select * from tbl_emails where email_type='Contact Us'";
	 $resEmail=mysql_query($sqlEmail);
	 $rowEmail=mysql_fetch_array($resEmail);
 
	 $message=str_replace('%name%',$_POST['txtName'],str_replace('%inquiry%',nl2br(stripslashes($_POST['txtQuery'])),$rowEmail['email_body']));
	 $headers = "From: Swapen.com<mail@swapen.com> \r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n";
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	 mail($_POST['txtEmail'],$subject, $message, $headers);

 
 $email_message="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
   <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$emailContent."</td>
  </tr>
  
 
</table>
";
		$to="vikas@adroitcoders.com";
		$filename=$_FILES['resume']['name'];
	if(move_uploaded_file($_FILES['resume']['tmp_name'],"resume/".$filename))
	{
	
	    $ourFileName="resume/".$filename;
	    mysql_query("update tbl_report set attachfile='$ourFileName' where report_id='$insertId'") or mysql_error();
	
	
	//$fp = fopen($ourFileName, "r"); 
    //$file = fread($fp, filesize($ourFileName)); 

    //$file = chunk_split(base64_encode($file)); 
	}
	
$fileatt = $ourFileName; // Path to the file 
$fileatt_type = "application/doc"; // File Type 
$fileatt_name =$filename; // Filename that will be used for the file as the attachment 
  
   $headers = "From: ".$_POST['txtEmail']; 

$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file); 

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

$headers .= "\nMIME-Version: 1.0\n" . 
"Content-Type: multipart/mixed;\n" . 
" boundary=\"{$mime_boundary}\""; 

$email_message .= "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . 
$email_message .= "\n\n"; 

$data = chunk_split(base64_encode($data)); 

$email_message .= "--{$mime_boundary}\n" . 
"Content-Type: {$fileatt_type};\n" . 
" name=\"{$fileatt_name}\"\n" . 
//"Content-Disposition: attachment;\n" . 
//" filename=\"{$fileatt_name}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data .= "\n\n" . 
"--{$mime_boundary}--\n"; 

mail($to, "Contact Us Enquiry", $email_message, $headers); 

  
   		$_SESSION['msg']="Messege sent successfully";
}
?>

<div id='panel'>
   <a name="cnt" id="cnt"></a>
<table width="100%" border="0">
  <tr>
    <td class="errorField" align="center"><?php echo $_SESSION['msg'];$_SESSION['msg']=""?></td>
  </tr>
  <tr>
    <td class="contentHeader"><h2>Contact Us</h2></td>
  </tr>
  <tr>
    <td><form action="" method="post" enctype="multipart/form-data" name="frmContact" id="frmContact" onsubmit="return contactValidate();">
      <table width="100%" border="0" cellpadding="5" cellspacing="3">
        <tr>
          <td colspan="4">We will require a username and password if you are contacting us in relation to your account </td>
          </tr>
        <tr>
          <td width="15%">Subject</td>
          <td><input name="txtSub" type="text" id="txtSub" size="40" class="input" /></td>
          <td>&nbsp;</td>
          <td><div align="right"><a href="javascript:void(0)" onclick="window.open('suggestions.php','welcome','width=500,height=300')">Suggestion Box</A></div></td>
        </tr>
        <tr>
          <td>Attach a File </td>
          <td colspan="3"><input type="file" name="resume" class="input" size="40" /></td>
        </tr>
        <tr>
          <td>Your Name </td>
          <td width="36%"><input name="txtName" type="text" id="txtName" class="input" size="40" /></td>
          <td width="10%">Your Email </td>
          <td width="39%"><input name="txtEmail" type="text" id="txtEmail" class="input" size="40" /></td>
        </tr>
        <tr>
          <td>Username</td>
          <td><input name="txtUser" type="text" id="txtUser" class="input" size="40" /></td>
          <td>Password</td>
          <td><input name="txtPass" type="password" id="txtPass" class="input" size="40" /></td>
        </tr>
        <tr>
          <td valign="top">Your Query </td>
          <td colspan="3"><textarea name="txtQuery" cols="40" rows="5" id="txtQuery" class="textarea"></textarea></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td colspan="3"><input class="button" type="submit" name="Submit" value="Send Message" /></td>
        </tr>
      </table>
        </form></td>
  </tr>
</table>
</div>
